package
{
	import flash.net.SharedObject;
	
	import org.flixel.*;
	import org.osmf.net.dynamicstreaming.INetStreamMetrics;
	
	public class LootState extends FlxState
	{
		protected var musicOff:Boolean;
		
		private static var furthestProgress:uint = 0;
		private static var INITIAL_POSITION_X:int = 70;
		private static var INITIAL_POSITION_Y:int = 100;
		private static var CELL_SIZE:int = 80;
		private static var ROWS:int = 6;
		private static var COLS:int = 7;

		protected var playedSoundOnce:Boolean;
		
		protected var localInformation:SharedObject;
		protected var theCurrentLevel:int;
		protected var toContinue:FlxText;
		protected var levels:FlxGroup;
		protected var mouse:FlxSprite;
		protected var timer:Number;
		protected var returnButton:FlxSprite;
		
		public function LootState(finishedLevel:int)
		{
			super();
			playedSoundOnce= false;

			localInformation = SharedObject.getLocal("elementalGameInfo");
			var level:String = localInformation.data["currentLevel"];
			theCurrentLevel = Math.max(int(level), finishedLevel);
			
			levels = new FlxGroup();
			
			timer = 0;
			
			mouse = new FlxSprite(FlxG.mouse.screenX,FlxG.mouse.screenY,Res.blankImage);
			
			returnButton = new FlxSprite(700-208, 640 - 126, Res.returnImg);
			if (theCurrentLevel <= 40) {
				this.add(new FlxSprite(0,0,Res.bg));
				this.add(new FlxSprite(82,20,Res.title));
			} else {
				this.add(new FlxSprite(0,0,Res.bgCleared));
				this.add(new FlxSprite(82,20,Res.titleCleared));
			}
			musicOff = true;
			toContinue = new FlxText(0, 600, 700, "Click on an unlocked level to play it.", false);
			
			toContinue.setFormat("helvetica", 25, 0xffffff, "center", 0x662222);
			
			this.add(toContinue);
			this.add(returnButton);
			if (finishedLevel == theCurrentLevel) {
				localInformation.data["currentLevel"] = "" + theCurrentLevel;
				localInformation.flush();
			}
			
			var cellNum:int = 0;
			for (var i:int = 0; i < ROWS; i++) {
				for (var j:int = 0; j < COLS; j++) {
					var index:int = (i*COLS) + j;
					if (index > 39) break;
					var pos:FlxPoint = getPointFromIndex(index);
					
					var xOffset:int = i*80;
					var yOffset:int = i*80;
					
					var levelImage:LootStateLevelImage;
					if (cellNum + 1 < theCurrentLevel) {
						levelImage = new LootStateLevelImage(INITIAL_POSITION_X + pos.x, INITIAL_POSITION_Y + pos.y, cellNum, 1);
					} else if (cellNum + 1 == theCurrentLevel) {
						levelImage = new LootStateLevelImage(INITIAL_POSITION_X + pos.x, INITIAL_POSITION_Y + pos.y, cellNum, 2);
					} else {
						levelImage = new LootStateLevelImage(INITIAL_POSITION_X + pos.x, INITIAL_POSITION_Y + pos.y, cellNum, 3);
					}
					levels.add(levelImage);
					cellNum++;
				}
			}
			
			this.add(levels);
		}
		
		/**
		 * Returns upper left point of cell index.
		 */
		public static function getPointFromIndex(index:int):FlxPoint {
			var row:int = index / COLS;
			var col:int = index - (row * COLS); 
			return new FlxPoint(col * CELL_SIZE, row * CELL_SIZE);
		}
		
		
		public override function update():void { 
			super.update(); 

			if (FlxG.mouse.justPressed() && returnButton.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
				FlxG.switchState(new MenuState());
			}
		}
	}
}